
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.FileInputStream;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.Timer;
import sun.audio.AudioData;
import sun.audio.AudioDataStream;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Riso
 */
public class Lodka extends JLabel implements KeyListener, ActionListener {

    Panel j;
    Timer t = new Timer(50, this);
//    ImageIcon lodka1 = new ImageIcon("img/lodka.png");
//    ImageIcon lodka2 = new ImageIcon("img/lodka2.png");
//    ImageIcon lodka1_zasah = new ImageIcon("img/lodka_explo1.png");
//    ImageIcon lodka2_zasah = new ImageIcon("img/lodka2_explo1.png");
//    ImageIcon lodka1_umry = new ImageIcon("img/lodka_kaput.png");
//    ImageIcon lodka2_umry = new ImageIcon("img/lodka2_kaput.png");
    ImageIcon lodka1 = new javax.swing.ImageIcon(getClass().getResource("/img/lodka.png"));
    ImageIcon lodka2 = new javax.swing.ImageIcon(getClass().getResource("/img/lodka2.png"));
    ImageIcon lodka1_zasah = new javax.swing.ImageIcon(getClass().getResource("/img/lodka_explo1.png"));
    ImageIcon lodka2_zasah = new javax.swing.ImageIcon(getClass().getResource("/img/lodka2_explo1.png"));
    ImageIcon lodka1_umry = new javax.swing.ImageIcon(getClass().getResource("/img/lodka_kaput.png"));
    int x;
    int y;
    int zivot;
    Rectangle rectangle;
    static ArrayList strely = new ArrayList();

    public Lodka(Panel j) {
        zivot = 3;
        this.setIcon(lodka1);
        this.setBounds(100, 65, 60, 30);
        this.j = j;
        rectangle = new Rectangle(100, 65, 60, 30);
        this.setFocusable(true);
        addKeyListener(this);
        t.start();
    }

    public void lodkastart(){
        this.setIcon(lodka1);
    }
    public void hudba() {
        AudioPlayer ap = AudioPlayer.player;
        AudioStream as;
        AudioData ad;
        AudioDataStream zvuk = null;
        try {
            as = new AudioStream(new FileInputStream("./zvuky/vybuch_lodky.wav"));
            ad = as.getData();
            zvuk = new AudioDataStream(ad);
        } catch (Exception e) {
        }
        ap.start(zvuk);
    }

    public void vystrel() {
        int x = getX() + 27;
        int y = getY() + 25;
        Strela s = new Strela(j, x, y);
        j.add(s);
        strely.add(s);
    }

    public static ArrayList getstrely() {
        return strely;
    }

    private void pohybvpravo() {
        if (getLocation().getX() < 340) {
            setLocation(getX() + 10, getY());
            rectangle.setLocation(this.getLocation());
            this.setIcon(lodka1);
        }
    }

    private void pohybvlavo() {
        if (getLocation().getX() > 1) {
            setLocation(getX() - 10, getY());
            rectangle.setLocation(this.getLocation());
            this.setIcon(lodka2);
        }

    }

    public void zasah() {
        hudba();
        if (zivot >= 1) {
            if (this.getIcon() == lodka1) {
                this.setIcon(lodka1_zasah);
            } else {
                this.setIcon(lodka2_zasah);
            }
        } else {
            this.setIcon(lodka1_umry);
            
        }
    }

    public void zastavpohyb() {
        keys.removeAll(keys);
    }
    private ArrayList<Integer> keys = new ArrayList<Integer>();

    public void keyTyped(KeyEvent e) {
    }

    public void keyPressed(KeyEvent e) {
        if (!keys.contains(new Integer(e.getKeyCode()))) {
            keys.add(new Integer(e.getKeyCode()));
        }
        int key = e.getKeyCode();
        switch (key) {
            case 75:
                vystrel();
                break;
        }
    }

    public void keyReleased(KeyEvent e) {
        keys.remove(new Integer(e.getKeyCode()));
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if (keys.contains(new Integer(65))) {
            pohybvlavo();
        }
        if (keys.contains(new Integer(68))) {
            pohybvpravo();
        }
    }
}
